package cn.hill4j.tool.spring.ext.iop.jdkproxy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.lang.reflect.Method;

/**
 * ClassName: JdkProxyIopClientBuilder
 * Description: jdk动态代理基础实现iopClient构造器
 * @author hillchen
 * Date: 2023/8/12
 * @version: 1.0
 */
public class SimpleJdkProxyIopClientBuilder extends JdkProxyIopClientBuilder implements ApplicationContextAware, InitializingBean {
    /**
     * spring 上下文容器
     */
    private ApplicationContext applicationContext ;
    /**
     * 代理方法执行器类型
     */
    private Class<? extends IopMethodInvokerHandler> methodInvoker;
    /**
     * 代理方法执行器
     */
    private IopMethodInvokerHandler invoker;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return invoker.invoke(proxy, method, args);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    /**
     * @author hillchen
     * Description: 设置代理方法执行器类型
     * Date: 2023/8/12 23:20
     * @param methodInvoker 代理方法执行器类型
     **/
    public void setMethodInvoker(Class<? extends IopMethodInvokerHandler> methodInvoker) {
        this.methodInvoker = methodInvoker;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        invoker = applicationContext.getBean(methodInvoker);
    }
}
